import 'package:flutter/material.dart';

/**
 * AlertDialog
 */
class AlertDialogDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return AlertDialogStateDemo();
  }
}

class AlertDialogStateDemo extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return AlertDialogState();
  }
}

enum Action { OK, Cancel }

class AlertDialogState extends State<AlertDialogStateDemo> {
  String _showContent = 'Nothing';

  Future _openAlertDialog() async {
    var actionValue = await showDialog(
      context: context,
      //强制用户点击按钮才能消失，点击空白处不消失
      barrierDismissible: false,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text('提示'),
          content: Text('提示具体内容'),
          actions: <Widget>[
            FlatButton(
              child: Text(
                'Cancel',
                style: TextStyle(color: Colors.grey),
              ),
              onPressed: () {
                Navigator.pop(context, Action.Cancel);
              },
            ),
            FlatButton(
              child: Text(
                'OK',
                style: TextStyle(color: Colors.blue),
              ),
              onPressed: () {
                Navigator.pop(context, Action.OK);
              },
            ),
          ],
        );
      },
    );

    switch (actionValue) {
      case Action.OK:
        setState(() {
          _showContent = 'OK';
        });
        break;
      case Action.Cancel:
        setState(() {
          _showContent = 'Cancel';
        });
        break;
      default:
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('AlertDialogDemo'),
        elevation: 0.0,
      ),
      body: Container(
        padding: EdgeInsets.all(8.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text('you are chose is $_showContent'),
            SizedBox(
              height: 16.0,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                RaisedButton(
                  child: Text('Open AlertDialog'),
                  onPressed: () {
                    _openAlertDialog();
                  },
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
